import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {BaseService} from '../../../shared/service/base.service';
import {ApiUrl} from '../../../config/api-url';
import {PageHelper} from '../../../utils/page-helper-model';

@Component({
  selector: 'app-aisle-device-type',
  templateUrl: './aisle-device-type.component.html',
  styleUrls: ['./aisle-device-type.component.less']
})
export class AisleDeviceTypeComponent implements OnInit {
  @Input()
  displayRackItemVisible = true;
  @Input()
  device: any;
  @Input()
  listOfData: any;
  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  // 分页
  @Input()
  pageHelper: PageHelper = new PageHelper;
  title: any;
  endHierarchy: number;
  startHierarchy: number;
  checked = false;
  loading = false;
  indeterminate = false;
  listOfCurrentPageData: any;
  setOfCheckedId = new Set<number>();
  maxValue = 0;
  hierarchyNum = 0;

  constructor(private baseService: BaseService) { }

  ngOnInit(): void {

  }
  ngOnChanges(): void{
    if(this.device != null || this.device != undefined){
      this.title = this.device?.deviceNo + '设备修改货道类型';
      this.findDisplayRackList(this.device);
    }
  }
  handleCancel(){
    this.displayRackItemVisible = false;
    this.pageHelper.currentPage = 1;
    this.emitVisible.emit(this.displayRackItemVisible);
    this.setOfCheckedId = new Set<number>();
    this.startHierarchy = null;
    this.endHierarchy = null;
    this.listOfData = [];
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findDisplayRackList(this.device);
  }

  onCurrentPageDataChange(listOfCurrentPageData: any): void {
    this.listOfCurrentPageData = listOfCurrentPageData;
  }
  onItemChecked(item: any, data: any): void {
    item.checked = !item.checked;
    if (item.checked){
      let i = 0;
      data.displayRackItemVoList.forEach(item2 => {
        if (item2.checked){
          i++;
        }
      });
      if (i== data.displayRackItemVoList.length){
        data.checked = true;
      }else {
        data.checked = false;
      }
      this.setOfCheckedId.add(item.id);
    }else {
      data.checked = item.checked;
      this.setOfCheckedId.delete(item.id);
    }
  }
  onGroupChecked(data: any): void {
    data.checked = !data.checked;
    data.displayRackItemVoList.forEach(item => {
      item.checked = data.checked;
      if (data.checked){
        this.setOfCheckedId.add(item.id);
      }else {
        this.setOfCheckedId.delete(item.id);
      }
    });
  }

  sendRequest23(): void {
    this.loading = true;
    this.updateDeviceTypeId(23);
  }
  sendRequest25(): void {
    this.loading = true;
    this.updateDeviceTypeId(25);
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findDisplayRackList(this.device);
  }
  findDisplayRackList(device) {
    if (device.id == null || device.id == ''){
      this.baseService.layer.messageError('设备号不能为空');
      return;
    }
    let params = {
      deviceId : device.id,
    };
    this.baseService.findListByMuchCondition(ApiUrl.DisplayRack + '/findDisplayRackList', params, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.pageHelper.numPages = data.data.pages;
          this.listOfData = data.data.list;
          this.maxValue = this.listOfData.length;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  getDeviceTypeName(deviceTypeId: number): string {
    switch (deviceTypeId) {
      case 23:
        return '弹簧';
      case 25:
        return '电磁';
      default:
        return ''; // 或者你可以选择返回其他默认值或文本
    }
  }
  updateDeviceTypeId(deviceTypeId) {
    if (this.device.id == null || this.device.id == ''){
      this.loading = false;
      this.baseService.layer.messageError('设备号不能为空');
      return;
    }
    let displayRackItemIdArray;
    if (this.startHierarchy){
      if (!this.endHierarchy ){
        this.endHierarchy = this.startHierarchy;
      }
      if (this.startHierarchy > this.listOfData.length || this.endHierarchy > this.listOfData.length){
        this.loading = false;
        this.baseService.layer.messageError('请输入有效货道层数(不能大于' + this.listOfData.length + ')');
        return;
      }
      if (this.startHierarchy <= 0 || this.endHierarchy <= 0){
        this.loading = false;
        this.baseService.layer.messageError('请输入有效货道层数');
        return;
      }

      let filteredData;
      filteredData = this.listOfData.filter(item => Number(item.hierarchy) >= Number(this.startHierarchy) && Number(item.hierarchy) <= Number(this.endHierarchy));
      if (filteredData.length < 1){
        filteredData = this.listOfData.filter(item => Number(item.hierarchy) >= Number(this.endHierarchy) && Number(item.hierarchy) <= Number(this.startHierarchy));
      }
      this.setOfCheckedId = new Set<number>();
      filteredData.forEach(item => {
        item.displayRackItemVoList.forEach(item2 => {
          this.setOfCheckedId.add(item2.id);
        });
      });
    }
    displayRackItemIdArray = Array.from(this.setOfCheckedId);
    let params = {
      deviceId : this.device.id,
      deviceTypeId,
      displayRackItemId: displayRackItemIdArray,
    };
    // this.loading = false;
    // this.baseService.layer.messageSuccess('保存成功');
    // this.handleCancel();
    this.baseService.post(ApiUrl.DisplayRackItem + '/updateDeviceTypeId', params).subscribe(
      data => {
        if (data.status == 0) {
          this.loading = false;
          this.baseService.layer.messageSuccess('保存成功');
          this.handleCancel();
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  onEndHierarchyChange(value: number): void {
    if (this.startHierarchy && this.endHierarchy) {
      // 确保两个值都不是null或undefined
      this.hierarchyNum = Math.abs(this.startHierarchy - this.endHierarchy) + 1;
    } else {
      // 至少有一个值为null或undefined，设置hierarchyNum为1
      this.hierarchyNum = 1;
    }
  }
}
